package com.guessdroid.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import android.os.Environment;

public class AndroidWriter {

	public static void writeTextToFile(String text, String fileName) {
		if (isStorageOk()) {
			try {
				File file = getFile(fileName);
				BufferedWriter writer = new BufferedWriter(new FileWriter(file,
						true));
				writer.write(text);
				writer.newLine();
				writer.flush();
				writer.close();
			} catch (IOException e) {
				throw new RuntimeException("Failed writing text", e);
			}
		} else {
			throw new RuntimeException("isStorageOk() was false");
		}
	}

	public static void deleteData() {
		if (isStorageOk()) {
			File contexts = getFile(Constants.CONTEXTS_FILE);
			File datapoints = getFile(Constants.DATAPOINTS_FILE);
			contexts.delete();
			datapoints.delete();
		} else {
			throw new RuntimeException("isStorageOk() was false");
		}
	}

	public static void writeObjectToFile(Object object, String fileName) {
		if (isStorageOk()) {
			try {
				File file = getFile(fileName);
				FileOutputStream fileOut = new FileOutputStream(file);
				ObjectOutputStream out = new ObjectOutputStream(fileOut);
				out.writeObject(object);
				out.close();
				fileOut.close();
			} catch (IOException e) {
				throw new RuntimeException("Failed writing object", e);
			}
		} else {
			throw new RuntimeException("isStorageOk() was false");
		}
	}

	public static Object readObjectFromFile(String fileName) {
		if (isStorageOk()) {
			try {
				File file = getFile(fileName);
				if (!file.isFile())
					return null;
				FileInputStream fileIn = new FileInputStream(file);
				ObjectInputStream in = new ObjectInputStream(fileIn);
				Object object = in.readObject();
				in.close();
				fileIn.close();
				return object;
			} catch (IOException e) {
				throw new RuntimeException("Failed reading object", e);
			} catch (ClassNotFoundException e) {
				throw new RuntimeException("Failed reading object", e);
			}
		} else {
			throw new RuntimeException("isStorageOk() was false");
		}
	}

	private static boolean isStorageOk() {
		boolean externalStorageAvailable = false;
		boolean externalStorageWriteable = false;
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
			externalStorageAvailable = externalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			externalStorageAvailable = true;
			externalStorageWriteable = false;
		} else { // Something else is wrong.
			externalStorageAvailable = externalStorageWriteable = false;
		}

		if (externalStorageAvailable && externalStorageWriteable)
			return true;
		else
			return false;
	}

	private static File getFile(String fileName) {
		File path = new File(Environment.getExternalStorageDirectory(),
				Constants.GUESSDROID_FILEPATH);
		path.mkdirs();
		return new File(path, fileName);
	}
}
